import { Controller, Get, Post, Body, Patch, Param, Delete, Query, Request, Header, ParseIntPipe } from "@nestjs/common";
import { ApiBearerAuth, ApiBody, ApiOperation, ApiParam, ApiQuery, ApiResponse, ApiTags } from "@nestjs/swagger";
import { TeacherService } from "./teacher.service";
import { CreateTeacherDto } from "./dto/create-teacher.dto";
import { UpdateTeacherDto } from "./dto/update-teacher.dto";
import type { QueryType } from "src/types/index.types";
import { TeacherTitleEnum } from "src/enums/school/teacher.enum";
import { CourseEnum, CourseLabelMap } from "src/enums/school/course.enum";

@Controller("/school/teacher")
@ApiTags("教师相关")
export class TeacherController {
  constructor(private readonly teacherService: TeacherService) {}

  @Post()
  @ApiOperation({ summary: "新增教师", description: "新增教师" })
  @ApiResponse({ status: 200, description: "success" })
  @ApiBody({ type: CreateTeacherDto })
  @ApiBearerAuth()
  create(@Body() createTeacherDto: CreateTeacherDto, @Request() request) {
    return this.teacherService.create(createTeacherDto, request);
  }

  @Get()
  @ApiOperation({ summary: "查询教师列表", description: "查询教师列表" })
  @ApiQuery({ name: "pageNum", required: false, description: "页码", type: Number })
  @ApiQuery({ name: "pageSize", required: false, description: "每页条数", type: Number })
  @ApiQuery({ name: "isAsc", required: false, description: "排序方式 desc-降序 asc-升序", enum: ["desc", "asc"] })
  @ApiQuery({ name: "teacherName", required: false, description: "教师姓名", type: String })
  @ApiQuery({
    name: "teacherTitle",
    required: false,
    description: `教师职称 ${Object.entries(TeacherTitleEnum).map(items => items[0] + "-" + items[1])}`,
    enum: Object.keys(TeacherTitleEnum),
  })
  @ApiQuery({
    name: "course",
    required: false,
    description: `负责课程 ${Object.entries(CourseLabelMap).map(items => items[0] + "-" + items[1])}`,
    enum: CourseEnum,
  })
  @ApiQuery({ name: "responsibleClass", required: false, description: "负责班级", type: String })
  @ApiResponse({ status: 200, description: "教师列表", type: CreateTeacherDto })
  @ApiBearerAuth()
  findAll(@Query() query: QueryType) {
    return this.teacherService.findAll(query);
  }

  @Get("options")
  @ApiOperation({ summary: "查询教师options列表", description: "查询教师options列表" })
  @ApiQuery({ name: "pageNum", required: false, description: "页码", type: Number })
  @ApiQuery({ name: "pageSize", required: false, description: "每页条数", type: Number })
  @ApiQuery({ name: "isAsc", required: false, description: "排序方式 desc-降序 asc-升序", enum: ["desc", "asc"] })
  @ApiResponse({ status: 200, description: "教师列表 {label:string; value:string | number}" })
  @ApiBearerAuth()
  findTeacherAllOptions(@Query() query: QueryType) {
    return this.teacherService.findTeacherAllOptions(query);
  }

  @Get("/export") // 详细路由必须在动态路由上方 如: @Get(":id")
  @Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  @Header("Content-Disposition", "attachment; filename=filename.xlsx")
  @ApiOperation({ summary: "导出教师", description: "导出教师" })
  @ApiResponse({ status: 200, description: "success", type: Buffer })
  @ApiBearerAuth()
  export() {
    return this.teacherService.export();
  }

  @Get(":id")
  @ApiOperation({ summary: "查询教师数据", description: "查询教师数据" })
  @ApiResponse({ status: 200, description: "success" })
  @ApiParam({ name: "id", type: Number })
  @ApiBearerAuth()
  findOne(@Param("id", new ParseIntPipe()) id: number) {
    return this.teacherService.findOne(id);
  }

  @Patch(":id")
  @ApiOperation({ summary: "更新教师", description: "更新教师" })
  @ApiResponse({ status: 200, description: "success" })
  @ApiParam({ name: "id", type: Number })
  @ApiBody({ type: CreateTeacherDto })
  @ApiBearerAuth()
  update(@Param("id", new ParseIntPipe()) id: number, @Body() updateTeacherDto: UpdateTeacherDto, @Request() request) {
    return this.teacherService.update(id, updateTeacherDto, request);
  }

  @Delete(":id")
  @ApiOperation({ summary: "删除教师", description: "删除教师" })
  @ApiResponse({ status: 200, description: "success" })
  @ApiParam({ name: "id", type: Number })
  @ApiBearerAuth()
  remove(@Param("id", new ParseIntPipe()) id: number) {
    return this.teacherService.remove(id);
  }
}

export { TeacherService };
